#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
unsigned long long pow10(unsigned n)
{
    //10的n次方
    if (n == 0)
        return 1;
    if (n == 1)
        return 10;
    unsigned long long p = pow10(n / 2);
    return (n % 2 == 0 ? 1 : 10) * p * p;
}
int reverse(int x)
{
    int tok = x > 0 ? 1 : -1;
    //int_max=tok==1?2147483647:-2147483648;
    char *p = new char[12];
    sprintf(p, "%d", x);
    if (tok < 0)
        p = &p[1]; //去掉符号
    int n = strlen(p);
    unsigned long long num = 0;
    for (int i = n - 1; i >= 0; i--)
    {
        num += (p[i] - '0') * pow10(i);
    }
    // cout<<num<<"     ";
    if (tok == 1)
    {
        if (num > (unsigned long long)2147483647)
            return 0;
    }
    if (tok == -1)
    {
        if (num > (unsigned long long)2147483648)
            return 0;
    }
    return (int)num * tok;
}
int main()
{
    cout << reverse(-2147483648) << endl;
    cout << reverse(-2147483412) << endl;
    cout << reverse(1563847412) << endl;
    cout << reverse(123) << endl;
    return 0;
}